package com.smartleanx.module.hrm.intergration;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.system.api.dept.DeptApi;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import com.smartleanx.module.system.api.dept.dto.SystemFrameVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 部门服务
 */
@Component
public class DeptServiceProvider {

    @Resource
    private DeptApi deptApi;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 返回部门指定id的部门Map
     *
     * @param ids
     * @return
     */
    public Map<Long, DeptRespDTO> getDeptMap(Collection<Long> ids) {
        return deptApi.getDeptMap(ids);
    }

    public List<DeptRespDTO> getDeptList(Collection<Long> ids) {
        return deptApi.getDeptList(ids).getCheckedData();
    }

    public DeptRespDTO getDept(Long id) {
        return deptApi.getDept(id).getCheckedData();
    }

    public List<DeptRespDTO> getDeptList() {
        return deptApi.getAllList().getCheckedData();
    }

    public Boolean deleteDept(Long id) {
        return deptApi.deleteDept(id).getCheckedData();
    }

    public List<DeptRespDTO> getChildDeptList(Long id) {
        return deptApi.getChildDeptList(id).getCheckedData();
    }

    public List<DeptRespDTO> getDeptTreeList() {
        return deptApi.getDeptList().getCheckedData();
    }


    public CommonResult<SystemFrameVo> getFrameByDepartmentId() {
        CommonResult<SystemFrameVo> frameByDepartmentId = deptApi.getFrameByDepartmentId();
        SystemFrameVo checkedData = frameByDepartmentId.getCheckedData();
        checkedData = ghUserId(checkedData);
        return CommonResult.success(checkedData);
    }

    public SystemFrameVo ghUserId(SystemFrameVo systemFrameVo) {
        if (!ObjectUtils.isEmpty(systemFrameVo)) {
            List<SystemFrameVo> systemFrameVos = systemFrameVo.getChildren();
            for (int i = 0; i < systemFrameVos.size(); i++) {
                if (systemFrameVos.get(i).getType() == 2) {
                    final int finalI = i;
                    EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
                        eq(EmployeeDO::getUserId, systemFrameVos.get(finalI).getId());
                        ne(EmployeeDO::getState, 6);
                    }});
                    if (!ObjectUtils.isEmpty(employeeDO)) {
                        systemFrameVos.get(finalI).setId(employeeDO.getUserId());
                    } else {
                        systemFrameVos.remove(finalI);
                    }
                    System.err.println(systemFrameVos);
                } else {
                    ghUserId(systemFrameVos.get(i));
                }
            }
            return systemFrameVo.setChildren(systemFrameVos);
        }
        return systemFrameVo;
    }
}
